package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UsersController {
    //配置属性是在api项目中，
    @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.sign}")
    private String smsSign;
    @Value("${sms.appkey}")
    private String smsAppkey;


    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @GetMapping("/get")
    public Object get(String id) {
        return userInfoService.get(id);
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone) {
        boolean ret = userInfoService.checkPhone(phone);

        return !ret;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone) {

        String limit = userInfoService.setLimit(phone);

        if (Integer.valueOf(limit) > 0) {
            userInfoService.decrLimit(phone);
//            String verifyCode = userInfoService.sendVerifyCode(phone, smsUrl, smsSign, smsAppkey);
            String vc = userInfoService.svc(phone);
            return new JsonResult(200, vc, null);
        } else {
            return new JsonResult(500, "发送验证码次数达到限制，请1分钟后再尝试", null);
        }
    }

    @PostMapping("/regist")
    public Object regist(UserInfo userInfo,String rpassword, String verifyCode) {
        userInfoService.regist(userInfo, rpassword, verifyCode);
        return JsonResult.success();
    }

    @PostMapping("/login")
    public Object login(String username, String password) {
        UserInfo userInfo = userInfoService.login(username, password);
        String token = userInfoRedisService.setToken(userInfo);
        //页面需要token user
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", userInfo);

        return JsonResult.success(map);
    }

    @RequireLogin //访问这个方法必须登录才行
    @PostMapping("/list")
    public Object list(HttpServletRequest request) {
        String token = request.getHeader("token");
        UserInfo user = userInfoRedisService.getUserByToken(token);

        return JsonResult.success(user);
    }

    @RequireLogin //访问这个方法必须登录才行
    @GetMapping("/info")
    public Object info(@UserParam UserInfo userInfo) {
        return JsonResult.success(userInfo);
    }

    @GetMapping("/strategies/favor")
    public Object querySidsFavor(String sid, String userId) {
        String key = RedisKeys.USER_SIDS.join(userId);
        List<String> favors = JSON.parseArray(userInfoRedisService.get(key), String.class);
        if (favors != null) {
            return JsonResult.success(favors);
        } else {
            return JsonResult.success();
        }
    }

    @GetMapping("/travels/favor")
    public Object queryTidsFavor(String uid) {
        String key = RedisKeys.USER_TIDS.join(uid);
        List<String> favors = JSON.parseArray(userInfoRedisService.get(key), String.class);
        if (favors != null) {
            return JsonResult.success(favors);
        } else {
            return JsonResult.success();
        }
    }

    @GetMapping("/homepage")
    public Object homepage(String userId, @UserParam UserInfo user) {
        Map<String, Object> map = new HashMap<>();
        UserInfo userInfo = userInfoService.get(userId);
        List<UserInfo> users = userInfoService.insertUid(userId, user.getId());
        String ttvKey = RedisKeys.USER_TOTALVIEW.join(userId);
        String tdvKey = RedisKeys.USER_TODAYVIEW.join(userId);
        String totalView = redisTemplate.opsForValue().get(ttvKey);
        String todayView = redisTemplate.opsForValue().get(tdvKey);
        System.out.println(totalView);
        map.put("user", userInfo);
        map.put("users", users);
        map.put("totalView", Integer.valueOf(totalView));
        map.put("todayView", Integer.valueOf(todayView));
        return JsonResult.success(map);
    }

    @GetMapping("/fans")
    public Object fans(String userId) {
        List<UserInfo> users = userInfoService.getUsers(userId);
        return JsonResult.success(users);
    }
}
